#include <stdlib.h>

#include "link_list_b.h"

void add_head_llb(Node* head, void* data)
{
    Node* node = create_n(data);
    node->next = head->next;
    head->next = node;
}

void add_tail_llb(Node* head, void* data)
{
    while (NULL != head->next)
    {
        head = head->next;
    }
    head->next = create_n(data);
}

void traversal_llb(Node* head, FUNC show)
{
    while (NULL != head->next)
    {
        show(head->next->data);
        head = head->next;
    }
    printf("\n");
}

void _del_n_llb(Node* node)
{
    Node* tmp = node->next;
    node->next = tmp->next;
    free(tmp);
}

bool del_head_llb(Node* head)
{
    if (NULL == head->next)
    {
        return false;
    }
    _del_n_llb(head);
    return true;
}

bool del_tail_llb(Node* head)
{
    while (NULL != head->next && NULL != head->next->next)
    {
        head = head->next;
    }
    return del_head_llb(head);
}

bool del_index_llb(Node* head, size_t index)
{
    while (index && NULL != head->next)
    {
        head = head->next;
        index--;
    }
    return del_head_llb(head);
}

bool del_val_llb(Node* head, void* val, CMP cmp)
{
    Node* n = query_val_llb(head, val, cmp);
    if (NULL == n->next)
    {
        return false;
    }
    return del_head_llb(n);
}
//插入
bool insert_llb(Node* head, size_t index, void* data)
{
    while (NULL != head->next)
    {
        if (0 == index--)
        {
            add_head_llb(head, data);
            return true;
        }
        head = head->next;
    }
    return false;
}
//查询
void* query_val_llb(Node* head, void* key, CMP cmp)
{
    while (NULL != head->next)
    {
        if (0 == cmp(head->next->data, key))
            break;
        head = head->next;
    }
    return head->data;
}

void* query_index_llb(Node* head, size_t index)
{
    while (NULL != head->next && index--)
    {
        head = head->next;
    }
    return head->data;
}

bool mod_index_llb(Node* head, size_t index, void* key)
{
    while (index && NULL != head->next)
    {
        head = head->next;
        index--;
    }
    if (NULL == head->next)
    {
        return false;
    }
    head->next->data = key;
    return true;
}

bool mod_val_llb(Node* head, void* old, void* new, CMP cmp)
{
    Node* n = query_val_llb(head, old, cmp);
    if (NULL == n->next)
    {
        return false;
    }
    n->next->data = new;
    return true;
}

void sort_llb(Node* head, CMP cmp)
{
    Node* end = NULL;
    for (Node* i = head->next, *j; NULL != i->next; i = i->next)
    {
        for (j = head->next; end != j->next; j = j->next)
        {
            if (0 < cmp(j->data, j->next->data))
            {
                int* t = j->data;
                j->data = j->next->data;
                j->next->data = t;
            }
        }
        end = j;
    }
}

size_t size_llb(Node* head)
{
    size_t len = 0;
    while (NULL != head->next)
    {
        head = head->next;
        len++;
    }
    return len;
}

void destory_list_b(Node* head)
{
    while (del_head_llb(head));
}

